package com.sxbbc.api.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.api.common.MappingUtils;
import com.sxbbc.api.service.ILoginService;
import com.sxbbc.common.basics.login.ThirdLoginDto;
import com.sxbbc.common.core.dto.jz.LoginRegisterVo;

@RestController

public class LoginController {
	@Autowired
	private ILoginService loginService;
	
	/**
	 * 发送登录验证码
	 * @param login
	 * @param type
	 * @return
	 */
	@RequestMapping(value=MappingUtils.SEND_LOGIN_CODE)
	public RestResponse sendLoginCode(String login,Integer type) {
		if (StringUtils.isBlank(login)) {
			return GetRest.getFail("请输入手机号！");
		}
		if (type == null) {
			return GetRest.getFail("请选择登录类型");
		}
		return loginService.sendLoginCode(login, type);
	}
	
	/**
	 * 登录
	 * @param loginVo 登录对象
	 * @return
	 */
	@RequestMapping(value=MappingUtils.LOGIN)
	public RestResponse doLogin(LoginRegisterVo loginVo) {
		if (loginVo.getChannel() == null) {
			return GetRest.getFail("登录来源渠道为空！");
		}
		return loginService.doLogin(loginVo);
	}

	/**
	 * 忘记密码 发送验证码
	 * @param loginVo 登录对象
	 * @author leehao
	 * @return com.function.common.RestResponse
	 */
	@RequestMapping(value = MappingUtils.SEND_FORGET_CODE)
	public RestResponse sendResetPasswordCode(LoginRegisterVo loginVo){
		if (loginVo.getType() == null) {
			return GetRest.getFail("请选择重置类型");
		}
		if (StringUtils.isBlank(loginVo.getLogin())) {
			return GetRest.getFail("请输入相关账号");
		}
		return loginService.sendResetPasswordCode(loginVo);
	}

	/**
	 * 重置密码
	 * @param loginRegisterVo 请看对象中注释
	 * @author leehao
	 * @return com.function.common.RestResponse
	 */
	@RequestMapping(value = MappingUtils.FORGET_PASSWORD)
	public RestResponse forgetPassword(LoginRegisterVo loginRegisterVo){
		if (loginRegisterVo.getType() == null) {
			return GetRest.getFail("来源异常！");
		}
		if (StringUtils.isBlank(loginRegisterVo.getLogin())) {
			return GetRest.getFail("请输入需要重置的账号");
		}
		if (StringUtils.isBlank(loginRegisterVo.getValidCode())) {
			return GetRest.getFail("请输入验证码");
		}
		if (StringUtils.isBlank(loginRegisterVo.getPassword())) {
			return GetRest.getFail("请输入新密码");
		}
		return loginService.handleResetPassword(loginRegisterVo);
	}
	
	/**
	 * 校验第三方登陆(如果已经登陆则返回token)
	 * @param thirdLoginDto 友盟第三方登陆参数
	 * @return 校验结果
	 * @author xiaoky
	 */
	@RequestMapping(value = MappingUtils.VALID_THIRD_LOGIN)
	public RestResponse validThirdLogin (ThirdLoginDto thirdLoginDto) {
		if (thirdLoginDto == null) {
			return GetRest.getFail("授权异常");
		}
		return loginService.validThirdLogin(thirdLoginDto);
	}
	
}
